<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Code;
use Curl\Curl;

class SendMsg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'msg:send {code_id}';
    
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '邮件任务.';
    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    public function handle()
    {
        $codeId = $this->argument('code_id');
        
        $code = $this->getCode($codeId);
        
        if($code->type == 89 ){
            $data1 = json_decode($code->content);
            $phone = $data1->phone;
        
            $content = '【青苜网】明天是您与［'.$data1->company.'］公司约定的面试时间，请做好相关准备。或登录青苜网查看详情。';
        
            $uri = 'http://114.215.140.42:8866/sms.aspx';
            $data = array(
                'action'    => 'send',//'overage',
                'userid'    => 54,
                'account'   => '1001005',
                'password'  => 'abcd1234',
                'mobile'    => $phone,
                'content'   => $content,
                'extno'     => ''
            );
            $curl  =  new Curl();
            $curl->post($uri, $data);
            $result = $curl->rawResponse;
            $dom = new \DOMDocument();
            try {
                $dom->loadXML($result);
                 
                $status =  $dom->getElementsByTagName('returnstatus')->item(0)->nodeValue;
                $remainpoint =  $dom->getElementsByTagName('remainpoint')->item(0)->nodeValue;
                $taskId =  $dom->getElementsByTagName('taskID')->item(0)->nodeValue;
                $succesCounts =  $dom->getElementsByTagName('successCounts')->item(0)->nodeValue;
                if($status == 'Success')
                {
                    $code->update(['status'=>2]);
                }
                else
                {
                    $code->update(['status'=>3]);
                   Log::info('短信发送失败');
                }
            } catch (\Exception $e) {
                 Log::error(['短信网关错误'=>$e->getMessage()]);
            }
        }
    }
    
    /**
     * 获取code.
     *
     * @param Int $codeId
     */
    private function getCode($codeId)
    {
        $code = Code::find($codeId);
    
        return $code;
    }
}

